<template>
	<view class="columnbox wdh-100 content">
		<view class="rowbox wdh-100 jus top-box">
			<image src="/static/img/shop.svg" mode="widthFix"></image>
			门店基础信息
		</view>
		<view class="rowbox  spb one-info">
			<view>门头照片</view>
			<view class="right-box img-list">
				<view class="rowbox add-img" @click="deletImg(index)" v-for="(item,index) in imgList" :key='index'>
					<image :src="item.url" mode="aspectFill"></image>
					<view class="rowbox delet-box">
						<u-icon name="close-circle-fill" size="32rpx" color="#000"></u-icon>
					</view>
				</view>
				<view class="rowbox add-img" @click="addImg">
					<u-icon name="plus"></u-icon>
				</view>
			</view>
		</view>
		<view class="rowbox  spb one-info">
			<view>门店名称</view>
			<input v-model="name" placeholder="请输入" />
		</view>
		<view class="rowbox  spb one-info" @click="chooseAddress">
			<view>详细地址</view>
			<view class="rowbox">{{address||'请选择'}}
				<view style="margin-left: 20rpx;"> <u-icon name="arrow-down" size="24rpx"></u-icon></view>
			</view>
		</view>
		<view class="rowbox  spb one-info">
			<view>营业时间</view>
			<picker @change="businessHoursChange" :range="businessHoursList" range-key="name">
				<view class="rowbox">
					{{businessHoursList[businessHours].name}}
					<view style="margin-left: 20rpx;"> <u-icon name="arrow-down" size="24rpx"></u-icon></view>
				</view>
			</picker>
		</view>
		<view class="rowbox  spb one-info">
			<view>站长电话</view>
			<input v-model="phone" placeholder="请输入" />
		</view>
		<view class="rowbox  spb one-info">
			<view>订单结束方式</view>
			<view class="rowbox">超时微信支付 </view>
		</view>
		<view class="rowbox  spb one-info">
			<view>洗车免费时长</view>
			<picker @change="freeTimeChange" :range="freeTimeList" range-key="name">
				<view class="rowbox">
					{{freeTimeList[freeTime].name}}
					<view style="margin-left: 20rpx;"> <u-icon name="arrow-down" size="24rpx"></u-icon></view>
				</view>
			</picker>
		</view>
		<view class="rowbox  spb one-info">
			<view>门店类型</view>
			<picker @change="stationTypeChange" :range="stationTypeList" range-key="name">
				<view class="rowbox">
					{{stationTypeList[stationType].name}}
					<view style="margin-left: 20rpx;"> <u-icon name="arrow-down" size="24rpx"></u-icon></view>
				</view>
			</picker>
		</view>
		<view class="rowbox  spb one-info">
			<view>门店权限</view>
			<picker @change="openTypeChange" :range="openTypeList" range-key="name">
				<view class="rowbox">
					{{openTypeList[openType].name}}
					<view style="margin-left: 20rpx;"> <u-icon name="arrow-down" size="24rpx"></u-icon></view>
				</view>
			</picker>
		</view>
		<view class="small-btn one-btn" @click="submitInfo">保存</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgList: [],
				lng: 0,
				lng: 0,
				address: '',
				name: '',
				businessHours: '',
				phone: '',
				freeTimeList: [{
						name: '1分钟',
						value: 1
					},
					{
						name: '2分钟',
						value: 2
					},
					{
						name: '3分钟',
						value: 3
					}
				],
				freeTime: 0,
				stationType: '',
				businessHoursList: [{
						name: '0-24h',
						value: 0
					},
					{
						name: '8-18h',
						value: 1
					}
				],
				businessHours: 0,
				stationTypeList: [{
						name: '旗舰站',
						value: 0
					},
					{
						name: '标准站',
						value: 1
					}, {
						name: '自助站',
						value: 2
					},
					{
						name: '自动站',
						value: 3
					}
				],
				stationType: 0,
				openTypeList: [{
						name: '对内开放',
						value: 0
					},
					{
						name: '对外开放',
						value: 1
					}
				],
				openType: 0,
				submitflag: false
			};
		},
		methods: {
			submitInfo() {
				if (!this.submitflag) {
					this.getUserInfo()
					this.submitflag = true
					uni.showLoading({
						title: '正在提交'
					})
					this.$req.get('/xcx/join/record').then(res => {
						this.$req.get(`/xcx/join/${res.data.data}`).then(res => {
							this.submitBasicInfo(res.data.data.joinType)
						})
					})
				}
			},
			getUserInfo() {
				this.$req.get('/xcx/user/getInfo').then(res => {
					uni.hideLoading()
					uni.setStorageSync('userInfo', res.data.data)
					this.$store.dispatch('actionsHandleUserInfoChange', res
						.data.data);
				})
			},
			async submitBasicInfo(joinType) {
				let list = []
				// for (var i = 0; i < this.imgList.length; i++) {
				//     list.push(await this.sendImg(this.imgList[i]))
				// }
				list.push(await this.sendImg(this.imgList[0]))
				this.$req.post('/xcx/stationVerify', {
					"listUrl": JSON.stringify(list),
					"name": this.name,
					"lng": this.lng,
					"lat": this.lat,
					"address": this.address,
					"businessHours": this.businessHours,
					"phone": this.phone,
					"endingMethod": 0,
					"freeTime": this.freeTimeList[this.freeTime].value,
					"stationType": this.stationType,
					"openType": this.openType,
					"openingType": 0,
					"verifyType": 0,
					"joinType": joinType
				}).then(res => {
					uni.hideLoading()
					this.submitflag = false
					if (res.data.code == 200) {
						uni.showModal({
							title: '提交申请成功',
							content: '请等待审核',
							success: () => {
								let pages =
									getCurrentPages(); //获取所有页面栈实例列表
								let nowPage = pages[pages.length -
									1]; //当前页页面实例
								let prevPage = pages[pages.length -
									2]; //上一页页面实例
								prevPage.$vm.refreshflag = true
								uni.navigateBack()
							}
						})
					} else {
						uni.showModal({
							title: res.data.msg,
							success: () => {}
						})
					}
				})
			},
			sendImg(v) {
				return new Promise((resolve, reject) => {
					this.$req.uploadImg(v.url).then(res => {
						resolve({
							url: res.data.url,
							name: res.data.fileName
						})
					})
				})
			},
			openTypeChange(e) {
				this.openType = e.detail.value
			},
			stationTypeChange(e) {
				this.stationType = e.detail.value
			},
			businessHoursChange(e) {
				this.businessHours = e.detail.value
			},
			freeTimeChange(e) {
				this.freeTime = e.detail.value
			},
			chooseAddress() {
				uni.chooseLocation({
					success: (res) => {
						this.lat = res.latitude
						this.lng = res.longitude
						this.address = res.address
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			addImg() {
				uni.chooseImage({
					success: (res) => {
						let list = []
						res.tempFilePaths.forEach(img => {
							list.push({
								newflag: true,
								url: img
							})
						})
						this.imgList = this.imgList.concat(list)
					}
				})
			},
			deletImg(index) {
				this.imgList.splice(index, 1)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		font-family: PingFang SC;
	}

	.one-info {
		width: 710rpx;
		padding: 32rpx 20rpx;
		border-bottom: #eef0f7 solid 1px;
		font-size: 28rpx;

		input {
			text-align: right;
			font-size: 28rpx;
		}
	}

	.right-box {
		width: 500rpx;
	}

	.img-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
	}

	.add-img {
		width: 120rpx;
		height: 120rpx;
		border: #c9ccd4 solid 1px;
		border-radius: 10rpx;
		margin-right: 20rpx;
		position: relative;
		margin-bottom: 10rpx;

		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 10rpx;
		}

		.delet-box {
			position: absolute;
			right: 0;
			top: 0;
			background: rgba(#fff, .7);
			border-radius: 100rpx;
		}
	}

	.top-box {
		padding: 20rpx 20rpx;
		font-size: 32rpx;
		color: #17698F;
		border-bottom: #eef0f7 solid 1px;

		image {
			width: 40rpx;
			height: 40rpx;
			margin-right: 10rpx;
		}
	}

	.one-btn {
		width: 710rpx;
		height: 90rpx;
		border-radius: 20rpx;
		margin: 40rpx 0;
	}
</style>